<?php
	class Common_CurrencyController extends Zend_Controller_Action
	{
		private $_Currency_Service;

		public function preDispatch(){
			$this->_Currency_Service = new Common_Service_Currency();
		}

		public function indexAction(){
			echo Cff::renderTpl("common/currency/index.tpl","layout");
		}

		public function listAction(){
			$intpage=$this->_request->getParam('page',1);
			$pageSize=$this->_request->getParam('pageSize');
			$currency_name = $this->_request->getParam("currency_name","");
			$currency_cn_name = $this->_request->getParam("currency_cn_name","");
			$currency_code = $this->_request->getParam("currency_code","");
			$currency_symbol_left = $this->_request->getParam("currency_symbol_left","");
			$condition = array(
				'currency_name' => $currency_name,
				'currency_cn_name'=> $currency_cn_name,
				'currency_code' => $currency_code,
				'currency_symbol_left' => $currency_symbol_left
			);
			$intpage = ($intpage<1)?1:$intpage;
			$data = array(
				'count'=> 0,
				'ask'=>0
			);
			$count = $this->_Currency_Service->getByCondition($condition,'count(*)',0,0);
			$rows=$this->_Currency_Service->getByCondition($condition,'*',$pageSize,$intpage);
			if(count($rows)>0){
				$data=array(
					'count'=>$count,
					'result'=>$rows,
					'ask'=>1
				);	
			}
			die(Zend_Json::encode($data));
		}

		public function infoAction(){
			$currency_id = $this->_request->getParam('currency_id');
			$result = array(
				'count'=>0,
				'result'=>null
			);
			$row = $this->_Currency_Service->get($currency_id);
			if(!empty($row)){
				$row=$row->toArray();
				$result = array(
					'count'=>1,
					'result'=>$row
				);
			}
			die(Zend_Json::encode($result));
		}

		public function deleteAction(){
			$currency_id = $this->_request->getParam('currency_id');
			$ret = 0;
			if(!empty($currency_id)){
				$rows=$this->_Currency_Service->delete($currency_id);
			}
			if($rows>0){
				$message['tag']=1;
			}else{
				$message['tag']=0;
			}
			die(Zend_Json::encode($message));
		}
		
		public function createAction(){
			$lang = Zend_Registry::get('lang');
			$currency_name = $this->_request->getParam('name',"");
			$currency_cn_name = $this->_request->getParam('cn_name',"");
			$currency_code = $this->_request->getParam('code',"");
			$currency_symbol_left = $this->_request->getParam('symbol_left',"");
			$currency_symbol_right = $this->_request->getParam('symbol_right',"");
			$currency_decimal_point = $this->_request->getParam("decimal_point","");
			$currency_thousands_point = $this->_request->getParam("thousands_point","");
			$currency_decimal_places = $this->_request->getParam("decimal_places","");
			$currency_rate = $this->_request->getParam("rate","");
			//$currency_update_time = $this->_request->getParam("update_time","");
			$row = array(
				'currency_name'=>$currency_name,
				'currency_cn_name' => $currency_cn_name,
				'currency_code' => $currency_code,
				'currency_symbol_left' => $currency_symbol_left,
				'currency_symbol_right' => $currency_symbol_right,
				'currency_decimal_point' => $currency_decimal_point,
				'currency_thousands_point' => $currency_thousands_point,
				'currency_decimal_places' => $currency_decimal_places,
				'currency_rate' => $currency_rate,
				'currency_update_time' => date("Y-m-d H:i:s")
			);
			$val = array(
				'currency_name'=>$currency_name,
				'currency_cn_name' => $currency_cn_name,
				'currency_code' => $currency_code,
				'currency_symbol_left' => $currency_symbol_left,				
				'currency_rate' => $currency_rate
				
			);
			$msg=array(
				'tag'=>0,
				'msg'=> $lang['operation'].$lang['fail']
			);
			
			$error = Common_Service_Currency::validator($val);
			if(!empty($error)){
				$msg['tag'] = -1;
				$msg['list']['error'] =$error;
				echo Zend_Json::encode($msg);
				exit ;
			}
			$ret = $this->_Currency_Service->create($row);
			if($ret>0){
				$msg['tag'] = 1;
				$msg['msg'] = $lang['operation'].$lang['success'];
			}
			die(Zend_Json::encode($msg));
		}

		public function editAction(){
			$lang = Zend_Registry::get('lang');
			$currency_id = $this->_request->getParam('currency_id');
			$currency_name = $this->_request->getParam('currency_name',"");
			$currency_cn_name = $this->_request->getParam('currency_cn_name',"");
			$currency_code = $this->_request->getParam('currency_code',"");
			$currency_symbol_left = $this->_request->getParam('currency_symbol_left',"");
			$currency_symbol_right = $this->_request->getParam('currency_symbol_right',"");
			$currency_decimal_point = $this->_request->getParam("currency_decimal_point","");
			$currency_thousands_point = $this->_request->getParam("currency_thousands_point","");
			$currency_decimal_places = $this->_request->getParam("currency_decimal_places","");
			$currency_rate = $this->_request->getParam("currency_rate","");
			$row = array(
				'currency_name'=>$currency_name,
				'currency_cn_name' => $currency_cn_name,
				'currency_code' => $currency_code,
				'currency_symbol_left' => $currency_symbol_left,
				'currency_symbol_right' => $currency_symbol_right,
				'currency_decimal_point' => $currency_decimal_point,
				'currency_thousands_point' => $currency_thousands_point,
				'currency_decimal_places' => $currency_decimal_places,
				'currency_rate' => $currency_rate,
				'currency_update_time' => date("Y-m-d H:i:s")
			);
			$msg=array(
				'tag'=>0,
				'msg'=> $lang['operation'].$lang['fail'],
			);

			$val = array(
				'currency_name'=>$currency_name,
				'currency_cn_name' => $currency_cn_name,
				'currency_code' => $currency_code,
				'currency_symbol_left' => $currency_symbol_left,				
				'currency_rate' => $currency_rate
				
			);
						
			$error = Common_Service_Currency::validator($val);

			if(!empty($error)){
				$msg['tag'] = -1;
				$msg['list']['error'] =$error;
				echo Zend_Json::encode($msg);
				exit ;
			}

			$ret = $this->_Currency_Service->update($row,$currency_id);
			if($ret){
				$msg = array(
					'tag' =>1,
					'msg' => $lang['operation'].$lang['success'],
				);
			}
			die(Zend_Json::encode($msg));
		}
	}